<template>
  <div class="cart-sku" ref="target">
    <div class="attrs">
      <span class="ellipsis" @click="toggle">{{attrs}}</span>
      <i class="iconfont icon-angle-down"></i>
    </div>
    <div class="layer" v-if="visable">
      <div class="loading" v-if="loading"></div>
      <GoodsSku v-if="Goods" :goods="Goods" :skuId="skuId" @change="changeCart"/>
      <XtxButton v-if="Goods" type="primary" size="mini" style="margin-left:60px" @click="submit()">确认</XtxButton>
      <!-- @change="changeCartGoods" -->
    </div>
  </div>
</template>
<script>
import { ref } from 'vue'
import { onClickOutside } from '@vueuse/core'
import GoodsSku from '@/views/goods/components/goodsSku.vue'
import { getSpecsAndSkus } from '@/api/cart'
export default {
    name: 'CartSku',
    components:{GoodsSku},
    props:{
        attrs:{
            type:String
        },
        skuId:{
            type:String
        }
    },
    setup(props,{emit}){
        // 需求:显示隐藏切换  点击空白隐藏
        const loading = ref(false)
        const visable = ref(false)
        const Goods = ref(null)
        const open = () => {
            loading.value = true
            visable.value = true
            getSpecsAndSkus(props.skuId).then(data=>{
                Goods.value = data.result
                console.log(Goods.value,'Goods')
                // emit('changeCart',Goods.value)
                loading.value = false
            })
        }
        const hide = () => {
            visable.value = false
            Goods.value = null
        }
        const toggle = () => {
            if(visable.value) {
                hide()
            }else{
                open()
            }
        }
        const target = ref(null)
        onClickOutside(target, () => {
            hide()
        })
        // 规格显示与提交
        const currSku = ref(null)
        const changeCart = (sku) => {
            currSku.value = sku
            // console.log(currSku.value,'currSku')
        }
        const submit = () => {
            if(currSku.value && currSku.value.skuId && currSku.value.skuId !== props.skuId){
                emit('changeCartGoods',currSku.value)
            }
        }
        return {visable, toggle, target, loading, Goods, submit, changeCart}
    }
}
</script>
<style scoped lang="less">
.cart-sku {
  height: 28px;
  border: 1px solid #f5f5f5;
  padding: 0 6px;
  position: relative;
  margin-top: 10px;
  display:inline-block;
  .attrs {
    line-height: 24px;
    display: flex;
    span {
      max-width: 230px;
      font-size: 14px;
      color: #999;
    }
    i {
      margin-left: 5px;
      font-size: 14px;
    }
  }
  .layer {
    position: absolute;
    left: -1px;
    top: 40px;
    z-index: 10;
    width: 400px;
    border: 1px solid @xtxColor;
    box-shadow: 2px 2px 4px lighten(@xtxColor,50%);
    background: #fff;
    border-radius: 4px;
    font-size: 14px;
    padding: 20px;
    &::before {
      content: "";
      width: 12px;
      height: 12px;
      border-left: 1px solid @xtxColor;
      border-top: 1px solid @xtxColor;
      position: absolute;
      left: 12px;
      top: -8px;
      background: #fff;
      transform: scale(.8,1) rotate(45deg);
    }
    .loading {
      height: 224px;
      background: url(../../../assets/images/loading.gif) no-repeat center;
    }
  }
}
</style>